package com.dindang.propertyfee.mvp.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dindang.propertyfee.R;
import com.dindang.propertyfee.app.utils.NodesTreeUtils;
import com.dindang.propertyfee.mvp.model.entity.Build;
import com.jess.arms.utils.LogUtils;
import com.longxi.gamer_base.widget.EmptyLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cxw on & 2018/11/8
 */
public class BuildSelectDialog extends Dialog {

    public BuildSelectDialog(@NonNull Context context) {
        super(context);
    }

    public BuildSelectDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BuildSelectDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private OnBuildListener listener;
        private String title;
        private List<Build.Nodes> selectNodesList = new ArrayList<>();
        private List<Build.Nodes> nodesList = new ArrayList<>();
        private BuildViewAdapter adapter;
        private Build.Building buildingParent;

        private Map<String, List<Build.Nodes>> mNodesListMap = new HashMap<>();
        private String mCNodevalueKey;
        private int mLevel = 0;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title){
            if("".equals(title)){
                this.title = "请设置标题";
            }else{
                this.title = title;
            }
            return this;
        }

        public Builder setBuildingParent(Build.Building buildingParent) {
            this.buildingParent = buildingParent;
            return this;
        }

        public Builder setProperty(OnBuildListener listener) {
            this.listener = listener;
            return this;
        }

        private TextView tv_back_up_level;
        private ImageView iv_dialog_close;
        private TextView tv_title;
        private EmptyLayout emptylayout;
        private RecyclerView rl;
        private TextView tv_cancle;
        private TextView tv_sure;

        private BuildSelectDialog dialog;

        public BuildSelectDialog create() {
            dialog = new BuildSelectDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.propertyfee_dialog_build_select, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            tv_back_up_level = view.findViewById(R.id.propertyfee_tv_back_up_level);
            iv_dialog_close = view.findViewById(R.id.propertyfee_iv_dialog_close);
            tv_title = view.findViewById(R.id.propertyfee_tv_title);
            emptylayout = view.findViewById(R.id.propertyfee_emptylayout);
            rl = view.findViewById(R.id.propertyfee_rl);
            tv_cancle = view.findViewById(R.id.propertyfee_tv_cancle);
            tv_sure = view.findViewById(R.id.propertyfee_tv_sure);

            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            tv_title.setText(title);
            iv_dialog_close.setOnClickListener(v -> dialog.dismiss());
            tv_cancle.setOnClickListener(v -> dialog.dismiss());
            tv_back_up_level.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mLevel<=0){
                        dialog.dismiss();
                    }else{
                        listener.onBack();
                    }
                }
            });
            tv_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getSelectNode();
                    selectNodesList = NodesTreeUtils.getChildsManyGroup(selectNodesList, 0);
                    listener.onSearchClick(buildingParent, selectNodesList);
                }
            });

            adapter = new BuildViewAdapter();
            rl.setAdapter(adapter);

            return dialog;
        }

        public void load(){
            rl.setVisibility(View.GONE);
            emptylayout.setEmptyStatus(emptylayout.STATUS_LOADING);
        }

        /**
         * 到下一级
         * @param nodes
         * @param cNodevalueKey
         * @param level
         */
        public void downLevel(List<Build.Nodes> nodes, String cNodevalueKey, int level){
            mCNodevalueKey = cNodevalueKey;
            mLevel = level;
            mNodesListMap.put(mCNodevalueKey + mLevel, nodes);
            showData();

        }

        /**
         * 到上一级
         */
        public void upLevel() {
            mNodesListMap.remove(mCNodevalueKey + mLevel);
            mLevel--;
            getSelectNode();
            showData();
        }

        public void getSelectNode(){
            selectNodesList.clear();
            for (String key : mNodesListMap.keySet()) {
                for (Build.Nodes nodes : mNodesListMap.get(key)) {
                    if(nodes.isSelect){
                        selectNodesList.add(nodes);
                    }
                }
            }
        }

        /**
         * 显示数据
         */
        public void showData(){
            nodesList = mNodesListMap.get(mCNodevalueKey + mLevel);
            adapter.notifyDataSetChanged();
            if(nodesList.size() > 0){
                rl.setVisibility(View.VISIBLE);
                emptylayout.hide();
            }else{
                emptylayout.setEmptyStatus(emptylayout.STATUS_NO_DATA);
            }
        }

        class BuildViewAdapter extends RecyclerView.Adapter<BuildViewAdapter.ViewHolder> {

            @NonNull
            @Override
            public BuildViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.propertyfee_item_build, parent, false);
                ViewHolder viewHolder = new ViewHolder(view);
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(@NonNull BuildViewAdapter.ViewHolder holder, int position) {
                Build.Nodes nodes = nodesList.get(position);
                holder.tv_name.setText(nodes.text);
                if(nodes.isSelect){
                    holder.iv_select.setImageResource(R.mipmap.propertyfee_ic_select_press);
                }else{
                    holder.iv_select.setImageResource(R.mipmap.propertyfee_ic_select_normal);
                }

                if(nodes.hasSon()){
                    holder.iv_arrow.setVisibility(View.VISIBLE);
                }else{
                    holder.iv_arrow.setVisibility(View.GONE);
                }

                holder.iv_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(nodes.isSelect){
                            selectNodesList.remove(nodes);
                            nodes.setSelect(false);
                        }else{
                            selectNodesList.add(nodes);
                            nodes.setSelect(true);
                        }

                        notifyItemChanged(position);
                    }
                });

                holder.constraintlayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(!selectNodesList.contains(nodes)){
                            selectNodesList.add(nodes);
                            nodes.setSelect(true);
                        }

                        if(nodes.hasSon()){
                            listener.onClick(nodes);
                        }

                        notifyItemChanged(position);
                    }
                });
            }

            @Override
            public int getItemCount() {
                return nodesList.size();
            }

            class ViewHolder extends RecyclerView.ViewHolder {
                ConstraintLayout constraintlayout;
                TextView tv_name;
                ImageView iv_select;
                ImageView iv_arrow;
                ViewHolder(View itemView) {
                    super(itemView);
                    constraintlayout = itemView.findViewById(R.id.propertyfee_constraintlayout);
                    tv_name = itemView.findViewById(R.id.propertyfee_tv_name);
                    iv_select = itemView.findViewById(R.id.propertyfee_iv_select);
                    iv_arrow = itemView.findViewById(R.id.propertyfee_iv_arrow);
                }
            }

        }

        public void dismiss(){
            if(dialog != null){
                dialog.dismiss();
            }
        }

        public void show(){
            if(dialog != null){
                dialog.show();
            }
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(layoutParams);
    }

    public interface OnBuildListener {
        void onSearchClick(Build.Building buildingParent, List<Build.Nodes> nodesList);
        void onClick(Build.Nodes nodes);
        void onBack();
    }

}
